package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zul.event.PagingEvent;

import com.lt.flashcard.model.entity.Flashcard;
import com.lt.flashcard.model.service.FlashcardService;
import com.lt.flashcard.model.service.Repository;

public class Mytest {

    private int page = 0;
    private int pagesize = 100;
    private List<Flashcard> fc;
    private List<Flashcard> curPage;

    @Init
    public void init() {
        FlashcardService srv = Repository.getInstance().getFlashcardService();
        fc = srv.getAllFlashcards();
        loadPage();
    }

    public void loadPage() {
        int offset = page * pagesize;
        int end = offset + pagesize - 1;
        if (end >= fc.size()) {
            end = fc.size() - 1;
        }
        curPage = fc.subList(offset, end + 1);
    }

    @NotifyChange("curPage")
    @Command
    public void pageing(@BindingParam("event") PagingEvent event) {
        page = event.getActivePage();
        loadPage();
    }

    public List<Flashcard> getFc() {
        return fc;
    }

    public void setFc(List<Flashcard> fc) {
        this.fc = fc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPagesize() {
        return pagesize;
    }

    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }

    public List<Flashcard> getCurPage() {
        return curPage;
    }

    public void setCurPage(List<Flashcard> curPage) {
        this.curPage = curPage;
    }
}
